﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using HotelManagement.Class;

namespace HotelManagement
{
    public partial class DeleteStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                displayStaffName();
            }
        }

        protected void btnDeleteStaffByID_Click(object sender, EventArgs e)
        {
           deleteStaffAndBankAccount();
            
        }
        protected void BtnGoBack_Click(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }

        private void displayStaffName()
        {

            lbStaffId.Items.Clear();
            string SelectSQL = "SELECT FirstName,LastName, StaffID FROM Staff";

            SqlDataReader Reader;

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();
                SqlCommand Cmd = new SqlCommand(SelectSQL, con);
                ListItem NewItem = new ListItem();

                con.Open();
                Reader = Cmd.ExecuteReader();
                while (Reader.Read())
                {
                    NewItem = new ListItem();
                    NewItem.Text = Reader["StaffID"].ToString() + " - " + Reader["FirstName"].ToString() + " " + Reader["LastName"].ToString();
                    NewItem.Value = Reader["StaffID"].ToString();
                    lbStaffId.Items.Add(NewItem);
                }
                Reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void deleteStaffAndBankAccount()
        {
            try
            {
                string DeleteStaff = lbStaffId.SelectedValue;

                //Call DeleteBankAccount method
                int row2 = BankAccountStaffDBManager.deleteBankAccountStaff(DeleteStaff);

                if (row2 > 0)
                {
                    //call DeleteStaff method
                    int row1 = StaffDBManager.deleteStaff(DeleteStaff);
                    if (row1 > 0)
                    {
                        lblStatus.Text = "Staff ID: " + lbStaffId.SelectedValue + " has been successfully deleted";
                    }
                }
                else
                {
                    lblStatus.Text = "Delete unsuccessful. Please select Staff to delete.";
                }
            }
            catch (Exception)
            {
                lblStatus.Text = "Sorry, This Staff cannot be deleted due to Foreign Key constraint. Please select another Staff";
            }

        }

        protected void btnGoBack_Click1(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }

        protected void lbStaffId_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}